package com.ruoyi.work.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.core.page.Pages;
import com.ruoyi.common.core.page.Query;
import com.ruoyi.common.query.QueryTool;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.work.domain.WorkOrderDetail;
import com.ruoyi.work.service.IWorkOrderDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.work.mapper.WorkOrderMapper;
import com.ruoyi.work.domain.WorkOrder;
import com.ruoyi.work.service.IWorkOrderService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 工单Service业务层处理
 *
 * @author xwj
 * @date 2024-08-13
 */
@Service
public class WorkOrderServiceImpl extends ServiceImpl<WorkOrderMapper, WorkOrder> implements IWorkOrderService {
    @Autowired
    private WorkOrderMapper workOrderMapper;
    @Autowired
    private IWorkOrderDetailService workOrderDetailService;

    /**
     * 查询工单
     *
     * @param id 工单主键
     * @return 工单
     */
    @Override
    public WorkOrder selectWorkOrderById(Long id) {
        return workOrderMapper.selectWorkOrderById(id);
    }

    /**
     * 查询工单列表
     *
     * @param workOrder 工单
     * @return 工单
     */
    @Override
    public List<WorkOrder> selectWorkOrderList(WorkOrder workOrder) {
        return workOrderMapper.selectWorkOrderList(workOrder);
    }

    /**
     * 新增工单
     *
     * @param workOrder 工单
     * @return 结果
     */
    @Override
    @Transactional
    public WorkOrder insertWorkOrder(WorkOrder workOrder) {

        save(workOrder);
        insertWorkOrderDetails(workOrder);
        return workOrder;
    }

    /**
     * 修改工单
     *
     * @param workOrder 工单
     * @return 结果
     */
    @Override
    @Transactional
    public WorkOrder updateWorkOrder(WorkOrder workOrder) {
        insertWorkOrderDetails(workOrder);
        updateById(workOrder);
        return workOrder;
    }


    @Override
    public void insertWorkOrderDetails(WorkOrder workOrder) {
        List<WorkOrderDetail> workOrderDetailList = workOrder.getWorkOrderDetailList();
        Long id = workOrder.getId();
        Date now = new Date();
        if (StringUtils.isNotNull(workOrderDetailList)) {
            for(WorkOrderDetail workOrderDetail : workOrderDetailList) {
                workOrderDetail.setWorkOrderId(id);

                if(workOrderDetail.getId() == null) {
                    workOrderDetail.setCreateTime(now);
                    workOrderDetailService.save(workOrderDetail);
                } else {
                    workOrderDetail.setUpdateTime(now);
                    workOrderDetailService.updateById(workOrderDetail);
                }
            }
        }

        // 要删除的数据
        List<Long> deleteIds = workOrder.getDeleteIds();
        if(deleteIds != null && deleteIds.size() > 0) {
            Long[] ids = deleteIds.toArray(new Long[0]);
            workOrderDetailService.deleteWorkOrderDetailByIds(ids);
        }

    }

    /**
     * 批量删除工单
     *
     * @param ids 需要删除的工单主键
     * @return 结果
     */
    @Override
    public int deleteWorkOrderByIds(Long[] ids) {
        return workOrderMapper.deleteWorkOrderByIds(ids);
    }

    /**
     * 条件删除
     *
     * @param workOrder 工单
     * @return
     */
    @Override
    public int delete(WorkOrder workOrder) {
        QueryWrapper<WorkOrder> qw = new QueryWrapper<>();
        QueryTool.condition(qw, workOrder);
        return baseMapper.delete(qw);
    }

    /**
     * mp分页
     *
     * @param workOrder 工单
     * @return
     */
    @Override
    public Pages<WorkOrder> queryPage(WorkOrder workOrder) {
        QueryWrapper<WorkOrder> qw = new QueryWrapper<>();
        QueryTool.condition(qw, workOrder);
        IPage<WorkOrder> ipage = new Query<WorkOrder>().getPage(workOrder);

        IPage<WorkOrder> page = this.page(
                ipage,
                qw
        );
        return new Pages<>(page);
    }

    /**
     * 删除工单信息
     *
     * @param id 工单主键
     * @return 结果
     */
    @Override
    public int deleteWorkOrderById(Long id) {
        return workOrderMapper.deleteWorkOrderById(id);
    }
}
